//
//  Content.m
//  etionLib
//
//  Created by xqls on 12-5-15.
//  Copyright (c) 2012年 GuangZhouXuanWu. All rights reserved.
//

#import "XLGContext.h"

NSString *const XLGContextUpdateInfo = @"updateinfo";
NSString *const XLGContextUpdateInfoRemind = @"remind";
NSString *const XLGContextUpdateInfoNewTag = @"newtag";
NSString *const XLGContextUpdateInfoForceUpdate = @"forceupdate";
NSString *const XLGContextUpdateInfoNewVersionName = @"newversionname";
NSString *const XLGContextUpdateInfoNewVersionHttp = @"versionhttp";
NSString *const XLGContextUpdateInfoTip = @"tip";

NSString *const XLGContextHTTPaddress = @"HTTPaddress";

NSString *const XLGContextFileHTTPAddress = @"fileHTTPaddress";

NSString *const XLGContextLastUser = @"lastuser";
NSString *const XLGContextLastUserDisplayKey = @"displaykey";
NSString *const XLGContextLastUserLoginKey = @"loginkey";
NSString *const XLGContextLastUserInfo = @"userinfo";
NSString *const XLGContextLastUserInfoUserNum = @"usernumber";
NSString *const XLGContextLastUserInfoMobileNum = @"mobilenumber";
NSString *const XLGContextLastUserInfoLoginName = @"loginname";
NSString *const XLGContextLastUserInfoNickName = @"nickname";

NSString *const XLGContentIsFirstRun = @"isfirstrun";

NSString *const ContextDBVersion = @"dbversion";
NSString *const ContextDBVersionName = @"name";
NSString *const ContextDBVersionVersion = @"version";


@interface XLGContext ()

@property(nonatomic,retain) NSArray *arSyscode;
@property(nonatomic,retain) NSArray *fileHTTPaddress;


@end

@implementation XLGContext

static XLGContext *s_context;

@synthesize m_nUsernumber;
@synthesize m_szAccountno;
@synthesize m_szPsw;
@synthesize m_szUsernumber;
@synthesize m_szUserpath;
@synthesize m_bIsoffline;

@synthesize m_szVersionname;
@synthesize m_nClienttypeno;
@synthesize m_nBuildno;
@synthesize m_nVersionno;
@synthesize m_bLogined;
@synthesize m_bIsofflinelogin;

@synthesize m_szDevicetoken;
@synthesize m_szUserdefaulteppath;
@synthesize m_bIsfirstrun;
@synthesize m_dicExtendkey;
@synthesize HTTPaddress = _HTTPAddress;
@synthesize arSyscode = _arSyscode;
@synthesize userType = _userType;
@synthesize fileHTTPaddress = _fileHTTPaddress;
@synthesize hadNewVsersion = _hadNewVsersion;
@synthesize forceUpdate = _forceUpdate;

+ (void)loadSyscfgplist:(NSString *)szFilepath
{
    [XLGContext clearContext];
    
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:szFilepath];

    s_context.m_dicLastuser = dic[XLGContextLastUser];

    s_context.m_szVersionname = dic[VERSIONNAMET];

    s_context.m_nVersionno = [dic[VERSIONNOT] integerValue];

    s_context.m_nBuildno = [dic[BUILDNOT] integerValue];

    s_context.m_nClienttypeno = [dic[CLIENTTYPENOT] integerValue];

    s_context.m_bIsfirstrun = [dic[XLGContentIsFirstRun] boolValue];

    s_context.m_dicExtendkey = dic[EXTENDKEY];
    
    s_context.HTTPaddress = dic[XLGContextHTTPaddress];
    
    s_context.fileHTTPaddress = dic[XLGContextFileHTTPAddress];
    
    NSDictionary *updateInfo = dic[XLGContextUpdateInfo];
    
    s_context.hadNewVsersion = [updateInfo[XLGContextUpdateInfoNewTag] boolValue];
    
    s_context.forceUpdate = [updateInfo[XLGContextUpdateInfoForceUpdate] boolValue];
}

- (void)setUserType:(XLGContextUserType)userType
{
    _userType = userType;
}

-(NSArray*)updateTip
{
    //注意，没有copy时，主工程为ARC时会报错，非ARC会内存泄露
    NSDictionary *dic = [XLGContext getDocumentSysCfgDictionary];
    return [dic[XLGContextUpdateInfo][XLGContextUpdateInfoTip] copy_mm] ;
}

-(NSString*)newVersionName
{
    //注意，没有copy时，主工程为ARC时会报错，非ARC会内存泄露
    NSDictionary *dic = [XLGContext getDocumentSysCfgDictionary];
    return [dic[XLGContextUpdateInfo][XLGContextUpdateInfoTip] copy_mm];
}

+ (void)saveFirstRun:(BOOL)bFirst
{
    s_context.m_bIsfirstrun = bFirst;
    NSString *pathDoc = [[XMSFileHelper documentPath] stringByAppendingPathComponent:SYSCFGFE];
    NSDictionary *dicDoc = [NSDictionary dictionaryWithContentsOfFile:pathDoc];
    NSMutableDictionary *dic = [[NSMutableDictionary new] autorelease_mm];
    [dic setDictionary:dicDoc];
    [dic setObject:[NSNumber numberWithBool:bFirst] forKey:XLGContentIsFirstRun];
    [dic writeToFile:pathDoc atomically:NO];
}

+ (BOOL)needUpdateSyscfg
{
    NSString *pathDoc = [[XMSFileHelper documentPath] stringByAppendingPathComponent:SYSCFGFE];
    NSDictionary *dicDoc = [NSDictionary dictionaryWithContentsOfFile:pathDoc];
    NSString *pathBundle = [[NSBundle mainBundle] pathForResource:SYSCFGF ofType:@"plist"];
    NSDictionary *dicBundle = [NSDictionary dictionaryWithContentsOfFile:pathBundle];
    if (nil != dicDoc)
    {
        NSInteger numDoc = [[dicDoc objectForKey:BUILDNOT] integerValue], numBundle = [[dicBundle objectForKey:BUILDNOT] integerValue];
        if (numBundle > numDoc)
        {
            return YES;
        }
    }
    return NO;
}

+ (NSArray*)needUpdateDatabase
{
    NSMutableArray *array = [[NSMutableArray new] autorelease_mm];
    NSString *pathDoc = [[XMSFileHelper documentPath] stringByAppendingPathComponent:SYSCFGFE];
    NSDictionary *dicDoc = [NSDictionary dictionaryWithContentsOfFile:pathDoc];
    NSString *pathBundle = [[NSBundle mainBundle] pathForResource:SYSCFGF ofType:@"plist"];
    NSDictionary *dicBundle = [NSDictionary dictionaryWithContentsOfFile:pathBundle];
    if (nil != dicDoc)
    {
        NSArray *verDoc = dicDoc[ContextDBVersion];
        NSArray *verBundle = dicBundle[ContextDBVersion];
        
        for(NSDictionary *bundleVer in verBundle)
        {
            BOOL find = NO;
            for(NSDictionary *docVer in verDoc)
            {
                if([bundleVer[ContextDBVersionName] isEqualToString:docVer[ContextDBVersionName]])
                {
                    find = YES;
                    if([bundleVer[ContextDBVersionVersion] isEqualToString:docVer[ContextDBVersionVersion]] == NO)
                        [array addObject:bundleVer[ContextDBVersionName]];
                    break;
                }
            }
            if(find == NO)
            {
                [array addObject:bundleVer[ContextDBVersionName]];
            }
        }
    }
    return array;
}

+ (void)updateSyscfgplistCompletion:(void (^)(NSInteger nOldbuildno, NSInteger nNewbuildno))complete
{
    NSString *pathDoc = [[XMSFileHelper documentPath] stringByAppendingPathComponent:SYSCFGFE];
    NSDictionary *dicDoc = [NSDictionary dictionaryWithContentsOfFile:pathDoc];
    NSString *pathBundle = [[NSBundle mainBundle] pathForResource:SYSCFGF ofType:@"plist"];
    NSDictionary *dicBundle = [NSDictionary dictionaryWithContentsOfFile:pathBundle];
    if (nil != dicDoc)
    {
        NSInteger versionNoDoc = [[dicDoc objectForKey:VERSIONNOT] integerValue], versionNoBundle = [[dicDoc objectForKey:VERSIONNOT] integerValue];
        NSInteger numDoc = [[dicDoc objectForKey:BUILDNOT] integerValue], numBundle = [[dicBundle objectForKey:BUILDNOT] integerValue];
        if (versionNoDoc != versionNoBundle || numBundle > numDoc)
        {
            NSString *szLastUserDoc = [dicDoc objectForKey:XLGContextLastUser];
            [XMSFileHelper deleteFileDir:pathDoc];
            NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:dicBundle];
            [dic setObject:szLastUserDoc forKey:XLGContextLastUser];
            [dic writeToFile:pathDoc atomically:NO];
        }
        complete(numDoc, numBundle);
    }
}

+ (NSDictionary*)getMainBundleSysCfgDictionary
{
    NSString *pathBundle = [[NSBundle mainBundle] pathForResource:SYSCFGF ofType:@"plist"];
    return [NSDictionary dictionaryWithContentsOfFile:pathBundle];
}

+ (NSDictionary*)getDocumentSysCfgDictionary
{
    NSString *pathDoc = [[XMSFileHelper documentPath] stringByAppendingPathComponent:SYSCFGFE];
    return [NSDictionary dictionaryWithContentsOfFile:pathDoc];
}

+ (void)saveSysCfgValue:(id)value forKey:(NSString*)key
{
    NSString *pathDoc = [[XMSFileHelper documentPath] stringByAppendingPathComponent:SYSCFGFE];
    NSMutableDictionary *dic = [[NSMutableDictionary new] autorelease_mm];
    [dic setDictionary:[XLGContext getDocumentSysCfgDictionary]];
    [dic setObject:value forKey:key];
    [dic writeToFile:pathDoc atomically:NO];
}

+ (void)saveSysCfgValue:(NSDictionary*)dicValue
{
    NSString *pathDoc = [[XMSFileHelper documentPath] stringByAppendingPathComponent:SYSCFGFE];
    NSMutableDictionary *dic = [[NSMutableDictionary new] autorelease_mm];
    [dic setDictionary:[XLGContext getDocumentSysCfgDictionary]];
    [dic setDictionary:dicValue];
    [dic writeToFile:pathDoc atomically:NO];
}

+ (NSString*)fileDownloadURL:(NSString*)fileName
{
    return fileName;
    //return [[XLGContext context].fileHTTPaddress[0] stringByAppendingPathComponent:fileName];
}

+ (NSString*)thumbImageDownloadURL:(NSString*)fileName
{
    return fileName;
    //return [[XLGContext context].fileHTTPaddress[1] stringByAppendingPathComponent:fileName];
}

+ (void)remindNewVersion:(BOOL)remind
{
    NSDictionary *dic = [XLGContext getDocumentSysCfgDictionary];
    dic[XLGContextUpdateInfo][XLGContextUpdateInfoRemind] = @(remind);
    [XLGContext saveSysCfgValue:dic[XLGContextUpdateInfo] forKey:XLGContextUpdateInfo];
}

+ (XLGContext *)context
{
    return s_context;
}

+ (void)releaseContext
{
    [s_context release_mm];
    s_context = nil;
}

+ (void)clearContext
{
    [s_context release_mm];
    s_context = [XLGContext new];
}

- (void)dealloc
{
    [m_szUserdefaulteppath release_mm];
    [_m_dicLastuser release_mm];
    [m_szUserpath release_mm];
    [m_szUsernumber release_mm];
    [m_szAccountno release_mm];
    [m_szPsw release_mm];
    [m_szVersionname release_mm];
    [m_szDevicetoken release_mm];
    [m_dicExtendkey release_mm];
    [_HTTPAddress release_mm];
    [_fileHTTPaddress release_mm];
    superdealloc_mm;
}

@end
